﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class NPS_app_MasterData_Reason : PageBase
{
    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            loadFormData();
            if (getMode == FormMode.Editor)
            {
                loadData();
                btnSave.Text = "Update";
                lblHeader.Text = "Master Data: Reason Edit";
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        Entities.NPSReason Reason = new Entities.NPSReason()
        {
            sxTitle = setStringSecurity(txtName.Text),
            sxDetail = setStringSecurity(txtDetail.Text),
            iNPSReasonCategoryID = Convert.ToInt32(ddlReasonCategory.SelectedValue)
        };
        if (getMode == FormMode.Editor)
        {
            Reason.sxUpdater = ICPUser.ID;
            Reason.iID = getItemID;
            updateReason(Reason);
        }
        else { saveReason(Reason); }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {

    }

    #endregion

    #region Audy's Custom Function
    protected void loadData()
    {
        //fetch data from DB
        Controller.NPSReasonController SRController = new Controller.NPSReasonController();
        Entities.NPSReason Reason = SRController.getNPSReason(getItemID,false);

        //set all data to forms
        txtName.Text = Reason.sxTitle;
        txtDetail.Text = Reason.sxDetail;
        ddlReasonCategory.SelectedValue = Reason.iNPSReasonCategoryID.ToString();
    }

    protected void loadFormData()
    {
        //load Activity Name Item
        Controller.NPSReasonCategoryController SRCController = new Controller.NPSReasonCategoryController();
        ddlReasonCategory.Items.Clear();
        ddlReasonCategory.DataSource = SRCController.NPSReasonCategoryList;
        ddlReasonCategory.DataBind();
    }

    protected bool saveReason(Entities.NPSReason Reason)
    {
        Controller.NPSReasonController ANController = new Controller.NPSReasonController();
        if (ANController.InsertNPSReasonData(Reason) == 0)
        { return false; }
        else { return true; }
    }

    protected void updateReason(Entities.NPSReason Reason)
    {
        Controller.NPSReasonController ANController = new Controller.NPSReasonController();
        ANController.UpdateNPSReasonData(Reason);
    }
    #endregion
}
